package com.fjasmin.locationutils;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Address;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import static android.content.ContentValues.TAG;

public class MainActivity extends AppCompatActivity {

    private LocationUtils locationUtils = null;

    private Button button;

    private TextView mTVGps;

    private double longitude = -1;
    private double latitude = -1;

    private static final int MSG_TEST_GPS = 0x1000045;

    private Handler mHandler;

    private String str_gps;

    public MainActivity(){
        this.mHandler = new MyHander();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        locationUtils = LocationUtils.getInstance(this);
        locationUtils.setAddressCallback(new LocationUtils.AddressCallback() {
            @Override
            public void onGetAddress(Address address) {

            }

            @Override
            public void onGetLocation(double lat, double lng) {
                MainActivity.this.latitude = lat;
                MainActivity.this.longitude = lng;
                str_gps = "精度： " + latitude + "维度：" + longitude;
                mHandler.sendEmptyMessage(MSG_TEST_GPS);
            }
        });
    }

    private void initView(){
        mTVGps = findViewById(R.id.tv_gps);

        button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                locationUtils.getLocation();

            }
        });
    }

    private class MyHander extends Handler {
        MyHander() {
        }

        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case MSG_TEST_GPS:
                    mTVGps.setText(str_gps);
                    break;
            }
        }
    }
}